//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FrameProviderBridge.h"
#include "StringUtilities.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TFrameProvider :: TFrameProvider()
{
}

TFrameProvider :: TFrameProvider( TFrameProvider* other)
{
   id   = other->id;
   name = other->name;
}

//-------------------------- Invoice Bridge ----------------------------------

TFrameProviderBridge :: TFrameProviderBridge( TDatabase * database)
{
   qryGet = new TQuery( NULL);
   qryGet->DatabaseName = database->DatabaseName;
   BuildGetQuery();

   qryAdd = new TQuery( NULL);
   qryAdd->DatabaseName = database->DatabaseName;
   BuildAddQuery();

   qryUpdate = new TQuery( NULL);
   qryUpdate->DatabaseName = database->DatabaseName;
   BuildUpdateQuery();

   qryDelete = new TQuery( NULL);
   qryDelete->DatabaseName = database->DatabaseName;
   BuildDeleteQuery();
               
   qryGetFrameProviders = new TQuery( NULL);
   qryGetFrameProviders->DatabaseName = database->DatabaseName;
   BuildGetFrameProvidersQuery();

   qryGetOrders = new TQuery( NULL);
   qryGetOrders->DatabaseName = database->DatabaseName;
   BuildGetOrdersQuery();

   qryGetRefunds = new TQuery( NULL);
   qryGetRefunds->DatabaseName = database->DatabaseName;
   BuildGetRefundsQuery();

   qryDeleteOrders = new TQuery( NULL);
   qryDeleteOrders->DatabaseName = database->DatabaseName;
   BuildDeleteOrdersQuery();        

   qryDeleteRefunds = new TQuery( NULL);
   qryDeleteRefunds->DatabaseName = database->DatabaseName;
   BuildDeleteRefundsQuery();        

   qryAddOrder = new TQuery( NULL);
   qryAddOrder->DatabaseName = database->DatabaseName;
   BuildAddOrderQuery();

   qryUpdateOrder = new TQuery( NULL);
   qryUpdateOrder->DatabaseName = database->DatabaseName;
   BuildUpdateOrderQuery();

   qryDeleteOrder = new TQuery( NULL);
   qryDeleteOrder->DatabaseName = database->DatabaseName;
   BuildDeleteOrderQuery();

   qryAddRefund = new TQuery( NULL);
   qryAddRefund->DatabaseName = database->DatabaseName;
   BuildAddRefundQuery();

   qryUpdateRefund = new TQuery( NULL);
   qryUpdateRefund->DatabaseName = database->DatabaseName;
   BuildUpdateRefundQuery();

   qryDeleteRefund = new TQuery( NULL);
   qryDeleteRefund->DatabaseName = database->DatabaseName;
   BuildDeleteRefundQuery();
}

TFrameProviderBridge :: ~TFrameProviderBridge()
{
   delete qryGetOrders;
   delete qryGetRefunds;
   delete qryDeleteOrders;
   delete qryDeleteRefunds;
   delete qryAddOrder;
   delete qryUpdateOrder;
   delete qryDeleteOrder;
   delete qryAddRefund;
   delete qryUpdateRefund;
   delete qryDeleteRefund;
   delete qryGetFrameProviders;
   delete qryUpdate;
   delete qryDelete;
   delete qryAdd;
   delete qryGet;
}

void TFrameProviderBridge :: BuildGetQuery()
{
   qryGet->SQL->Add( "SELECT * FROM FRAMEPROVIDERS");
   qryGet->SQL->Add( "WHERE (ID = :prmID)");
}

void TFrameProviderBridge :: BuildAddQuery()
{
   qryAdd->SQL->Add( "INSERT INTO FRAMEPROVIDERS (NAME, ADDRESS, PHONE) VALUES (:prmName, :prmAddress, :prmPhone)");
}

void TFrameProviderBridge :: BuildUpdateQuery()
{
   qryUpdate->SQL->Add( "UPDATE FRAMEPROVIDERS");
   qryUpdate->SQL->Add( "SET NAME = :prmName, ADDRESS = :prmAddress, PHONE = :prmPhone");
   qryUpdate->SQL->Add( "WHERE (ID = :prmID)");
}

void TFrameProviderBridge :: BuildDeleteQuery()
{
   qryDelete->SQL->Add( "DELETE FROM FRAMEPROVIDERS WHERE");
   qryDelete->SQL->Add( "(ID = :prmID)");
}

void TFrameProviderBridge :: BuildGetFrameProvidersQuery()
{
   qryGetFrameProviders->SQL->Add( "SELECT * FROM FRAMEPROVIDERS");
   qryGetFrameProviders->SQL->Add( "ORDER BY NAME");
}


void TFrameProviderBridge :: BuildGetOrdersQuery()
{
   qryGetOrders->SQL->Add( "SELECT * FROM FRAMEORDERS");
   qryGetOrders->SQL->Add( "WHERE FRAME_PROVIDER_ID = :prmFrameProviderID");
   qryGetOrders->SQL->Add( "ORDER BY ORDER_DATE DESC, ID DESC");
}

void TFrameProviderBridge :: BuildGetRefundsQuery()
{
   qryGetRefunds->SQL->Add( "SELECT * FROM FRAMEREFUNDS");
   qryGetRefunds->SQL->Add( "WHERE FRAME_PROVIDER_ID = :prmFrameProviderID");
   qryGetRefunds->SQL->Add( "ORDER BY REFUND_DATE DESC, ID DESC");
}

void TFrameProviderBridge :: BuildDeleteOrdersQuery()
{
   qryDeleteOrders->SQL->Add( "DELETE FROM FRAMEORDERS");
   qryDeleteOrders->SQL->Add( "WHERE FRAME_PROVIDER_ID = :prmFrameProviderID");
}

void TFrameProviderBridge :: BuildDeleteRefundsQuery()
{
   qryDeleteRefunds->SQL->Add( "DELETE FROM FRAMEREFUNDS");
   qryDeleteRefunds->SQL->Add( "WHERE FRAME_PROVIDER_ID = :prmFrameProviderID");
}

void TFrameProviderBridge :: BuildAddOrderQuery()
{
   qryAddOrder->SQL->Add( "INSERT INTO FRAMEORDERS (FRAME_PROVIDER_ID, ORDER_DATE, INVOICE_NUMBER, AMOUNT)");
   qryAddOrder->SQL->Add( "VALUES (:prmFrameProviderID, :prmOrderDate, :prmInvoiceNo, :prmAmount)");
}

void TFrameProviderBridge :: BuildUpdateOrderQuery()
{
   qryUpdateOrder->SQL->Add( "UPDATE FRAMEORDERS");
   qryUpdateOrder->SQL->Add( "SET FRAME_PROVIDER_ID = :prmFrameProviderID,");
   qryUpdateOrder->SQL->Add( "ORDER_DATE = :prmOrderDate,");
   qryUpdateOrder->SQL->Add( "INVOICE_NUMBER = :prmInvoiceNo,");
   qryUpdateOrder->SQL->Add( "AMOUNT = :prmAmount");
   qryUpdateOrder->SQL->Add( "WHERE ID = :prmID");
}

void TFrameProviderBridge :: BuildDeleteOrderQuery()
{
   qryDeleteOrder->SQL->Add( "DELETE FROM FRAMEORDERS");
   qryDeleteOrder->SQL->Add( "WHERE ID = :prmID");
}

void TFrameProviderBridge :: BuildAddRefundQuery()
{
   qryAddRefund->SQL->Add( "INSERT INTO FRAMEREFUNDS (FRAME_PROVIDER_ID, REFUND_DATE, AMOUNT)");
   qryAddRefund->SQL->Add( "VALUES (:prmFrameProviderID, :prmRefundDate, :prmAmount)");
}

void TFrameProviderBridge :: BuildUpdateRefundQuery()
{
   qryUpdateRefund->SQL->Add( "UPDATE FRAMEREFUNDS");
   qryUpdateRefund->SQL->Add( "SET FRAME_PROVIDER_ID = :prmFrameProviderID,");
   qryUpdateRefund->SQL->Add( "REFUND_DATE = :prmRefundDate,");
   qryUpdateRefund->SQL->Add( "AMOUNT = :prmAmount");
   qryUpdateRefund->SQL->Add( "WHERE ID = :prmID");
}

void TFrameProviderBridge :: BuildDeleteRefundQuery()
{
   qryDeleteRefund->SQL->Add( "DELETE FROM FRAMEREFUNDS");
   qryDeleteRefund->SQL->Add( "WHERE ID = :prmID");
}

void SetFrameProvider( TQuery* query, TFrameProvider* provider)
{
   provider->id      = query->FieldByName( "ID")->AsInteger;
   provider->name    = query->FieldByName( "NAME")->AsString;
   provider->address = query->FieldByName( "ADDRESS")->AsString;
   provider->phone   = query->FieldByName( "PHONE")->AsString;
}

void TFrameProviderBridge :: Get( __int64 id, TFrameProvider* provider)
{
   qryGet->ParamByName( "prmID")->AsInteger = id;

   qryGet->Open();

   try
   {
      SetFrameProvider( qryGet, provider);
   }
   __finally
   {
      qryGet->Close();
   }
}

void TFrameProviderBridge :: GetProviders( TStrings * providers)
{
   TFrameProvider * provider;
   
   qryGetFrameProviders->Open();

   try
   {
      TStringUtilites :: Clear( providers);

      qryGetFrameProviders->First();

      while ( !qryGetFrameProviders->Eof)
      {
         provider = new TFrameProvider();
         SetFrameProvider( qryGetFrameProviders, provider);
         providers->AddObject( provider->name, provider);
         qryGetFrameProviders->Next();
      }
   }
   __finally
   {
      qryGetFrameProviders->Close();
   }
}

void TFrameProviderBridge :: Add( TFrameProvider* provider)
{
   qryAdd->ParamByName( "prmName")->AsString    = provider->name;
   qryAdd->ParamByName( "prmAddress")->AsString = provider->address;
   qryAdd->ParamByName( "prmPhone")->AsString   = provider->phone;

   qryAdd->ExecSQL();
}

void TFrameProviderBridge :: Update( TFrameProvider* provider)
{
   qryUpdate->ParamByName( "prmID")->AsInteger = provider->id;
   qryUpdate->ParamByName( "prmName")->AsString = provider->name;
   qryUpdate->ParamByName( "prmAddress")->AsString = provider->address;
   qryUpdate->ParamByName( "prmPhone")->AsString   = provider->phone;

   qryUpdate->ExecSQL();
}

void TFrameProviderBridge :: Delete( __int64 id)
{
   qryDelete->ParamByName( "prmID")->AsInteger = id;
   qryDelete->ExecSQL();

   qryDeleteOrders->ParamByName( "prmFrameProviderID")->AsInteger = id;
   qryDeleteOrders->ExecSQL();

   qryDeleteRefunds->ParamByName( "prmFrameProviderID")->AsInteger = id;
   qryDeleteRefunds->ExecSQL();
}

TFrameOrder :: TFrameOrder()
{
}

TFrameOrder :: TFrameOrder( TFrameOrder* other)
{
   id            = other->id;
   frameProviderID = other->frameProviderID;
   orderDate     = other->orderDate;
   invoiceNumber = other->invoiceNumber;
   amount        = other->amount;
}

bool TFrameOrder :: IsEqual( TFrameOrder* other)
{
   return ( id == other->id);
}

AnsiString TFrameOrder :: GetKey()
{
   return DateToStr( orderDate);        
}


void SetFrameOrder( TQuery* query, TFrameOrder* order)
{
   order->id   = query->FieldByName( "ID")->AsInteger;
   order->frameProviderID = query->FieldByName( "FRAME_PROVIDER_ID")->AsInteger;
   order->orderDate = query->FieldByName( "ORDER_DATE")->AsDateTime;
   order->invoiceNumber = query->FieldByName( "INVOICE_NUMBER")->AsInteger;
   order->amount = query->FieldByName( "AMOUNT")->AsCurrency;     
}

void TFrameProviderBridge :: GetOrders(__int64 frameProviderID, TStrings * orders)
{
   TFrameOrder * order;

   qryGetOrders->ParamByName("prmFrameProviderID")->AsInteger = frameProviderID;
   qryGetOrders->Open();

   try
   {
      TStringUtilites :: Clear( orders);

      qryGetOrders->First();

      while ( !qryGetOrders->Eof)
      {
         order = new TFrameOrder();
         SetFrameOrder( qryGetOrders, order);

         orders->AddObject( order->GetKey(), order);
         qryGetOrders->Next();
      }
   }
   __finally
   {
      qryGetOrders->Close();
   }
}

TFrameRefund :: TFrameRefund()
{
}

TFrameRefund :: TFrameRefund( TFrameRefund* other)
{
   id              = other->id;
   frameProviderID = other->frameProviderID;
   refundDate      = other->refundDate;
   amount          = other->amount;
}

bool TFrameRefund :: IsEqual( TFrameRefund* other)
{
   return ( id == other->id);
}

AnsiString TFrameRefund :: GetKey()
{
   return DateToStr( refundDate);        
}


void SetFrameRefund( TQuery* query, TFrameRefund* refund)
{
   refund->id         = query->FieldByName( "ID")->AsInteger;
   refund->frameProviderID = query->FieldByName( "FRAME_PROVIDER_ID")->AsInteger;
   refund->refundDate = query->FieldByName( "REFUND_DATE")->AsDateTime;
   refund->amount     = query->FieldByName( "AMOUNT")->AsCurrency;
}

void TFrameProviderBridge :: GetRefunds(__int64 frameProviderID, TStrings * refunds)
{
   TFrameRefund * refund;

   qryGetRefunds->ParamByName("prmFrameProviderID")->AsInteger = frameProviderID;
   qryGetRefunds->Open();

   try
   {
      TStringUtilites :: Clear( refunds);

      qryGetRefunds->First();

      while ( !qryGetRefunds->Eof)
      {
         refund = new TFrameRefund();
         SetFrameRefund( qryGetRefunds, refund);

         refunds->AddObject( refund->GetKey(), refund);
         qryGetRefunds->Next();
      }
   }
   __finally
   {
      qryGetRefunds->Close();
   }
}

void TFrameProviderBridge :: AddOrder( TFrameOrder* order)
{
   qryAddOrder->ParamByName( "prmFrameProviderID")->AsInteger = order->frameProviderID;
   qryAddOrder->ParamByName( "prmOrderDate")->AsDateTime = order->orderDate;
   qryAddOrder->ParamByName( "prmInvoiceNo")->AsInteger = order->invoiceNumber;
   qryAddOrder->ParamByName( "prmAmount")->AsCurrency = order->amount;
   qryAddOrder->ExecSQL();
}

void TFrameProviderBridge :: UpdateOrder( __int64 id, TFrameOrder* order)
{
   qryUpdateOrder->ParamByName( "prmFrameProviderID")->AsInteger = order->frameProviderID;
   qryUpdateOrder->ParamByName( "prmOrderDate")->AsDateTime = order->orderDate;
   qryUpdateOrder->ParamByName( "prmInvoiceNo")->AsInteger = order->invoiceNumber;
   qryUpdateOrder->ParamByName( "prmAmount")->AsCurrency = order->amount;
   qryUpdateOrder->ParamByName( "prmID")->AsInteger = order->id;
   qryUpdateOrder->ExecSQL();
}

void TFrameProviderBridge :: DeleteOrder( __int64 id)
{
   qryDeleteOrder->ParamByName( "prmID")->AsInteger = id;
   qryDeleteOrder->ExecSQL();
}

void TFrameProviderBridge :: AddRefund( TFrameRefund* refund)
{
   qryAddRefund->ParamByName( "prmFrameProviderID")->AsInteger = refund->frameProviderID;
   qryAddRefund->ParamByName( "prmRefundDate")->AsDateTime = refund->refundDate;
   qryAddRefund->ParamByName( "prmAmount")->AsCurrency = refund->amount;
   qryAddRefund->ExecSQL();
}

void TFrameProviderBridge :: UpdateRefund( __int64 id, TFrameRefund* refund)
{
   qryUpdateRefund->ParamByName( "prmFrameProviderID")->AsInteger = refund->frameProviderID;
   qryUpdateRefund->ParamByName( "prmRefundDate")->AsDateTime = refund->refundDate;
   qryUpdateRefund->ParamByName( "prmAmount")->AsCurrency = refund->amount;
   qryUpdateRefund->ParamByName( "prmID")->AsInteger = refund->id;
   qryUpdateRefund->ExecSQL();
}

void TFrameProviderBridge :: DeleteRefund( __int64 id)
{
   qryDeleteRefund->ParamByName( "prmID")->AsInteger = id;
   qryDeleteRefund->ExecSQL();
}

